#include "complex.h"
#include<malloc.h>
ComplexLink *BuyNode(DataType data)
{
  ComplexLink *pNewNode = (ComplexLink *)malloc(sizeof(ComplexLink));
  pNewNode->data = data;
  pNewNode->next = NULL;
  pNewNode->random =NULL;
  return pNewNode;
}

ComplexLink * CopyComplexLink(ComplexLink *first)
{
  if(first == NULL)
   return NULL;
  ComplexLink *p1 = first;
  
  //对data复制
  while(p1)
  {
    ComplexLink *pNewNode = BuyNode(p1->data);
    pNewNode->next = p1->next;
    p1->next = pNewNode;
    p1 = pNewNode->next;
  }
  ComplexLink *pNew = first->next;
  //对random的复制
  p1 = first;
  
  ComplexLink *p2 = NULL;
  while(p1)
  {
    p2 = p1->next;
    if(p1->random == NULL)
    {
      p2->random = NULL;
    }
    else 
     p2->random = p1->random->next;
    p1 = p2->next;
  }

  // 拆分
  
  p1 = first;
  p2 = p1->next;
  while(p2)
  {
    p1->next = p2->next;
    p1 = p2;
    p2 = p2->next;
  }
  return pNew;
 
}
